package com.mpri.aio.system.websocket;

import java.util.Map;

import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

public class WebSocketInterceptor implements HandshakeInterceptor {

	// 进入hander之前的拦截
	@Override
	public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse serverHttpResponse,
			WebSocketHandler webSocketHandler, Map<String, Object> map) throws Exception {
		if (request instanceof ServletServerHttpRequest) {
			String ID = request.getURI().toString().split("ID=")[1];
			System.out.println("当前session的ID=" + ID);
			ServletServerHttpRequest serverHttpRequest = (ServletServerHttpRequest) request;
			// HttpSession session = serverHttpRequest.getServletRequest().getSession();
			map.put("WEBSOCKET_USERID", ID);
		}
		return true;
	}

	@Override
	public void afterHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse,
			WebSocketHandler webSocketHandler, Exception e) {
		System.out.println("进来webSocket的afterHandshake拦截器！");
	}
}
